/**create by liuhua at 2019年6月10日 下午2:09:29**/
package com.star.truffle.module.fzjz.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springframework.context.ApplicationContext;

import com.star.truffle.common.importdata.AbstractDataExport;
import com.star.truffle.common.importdata.ExcelUtil;
import com.star.truffle.common.properties.Excel;
import com.star.truffle.core.jdbc.Page;
import com.star.truffle.core.util.DateUtils;
import com.star.truffle.module.fzjz.cache.PictureInfoCache;
import com.star.truffle.module.fzjz.cache.WarCategoryCache;
import com.star.truffle.module.fzjz.cache.WarInfoCache;
import com.star.truffle.module.fzjz.constant.PictureBusinessEnum;
import com.star.truffle.module.fzjz.domain.PictureInfo;
import com.star.truffle.module.fzjz.domain.WarInfo;
import com.star.truffle.module.fzjz.dto.req.WarInfoRequestDto;
import com.star.truffle.module.fzjz.dto.res.WarCategoryResponseDto;
import com.star.truffle.module.fzjz.dto.res.WarInfoResponseDto;

public class ExportWarInfo extends AbstractDataExport<WarInfo> {

  private WarInfoCache warInfoCache;
  private WarCategoryCache warCategoryCache;
  private PictureInfoCache pictureInfoCache;
  
  @Override
  public void getApplication(ApplicationContext applicationContext) {
    warInfoCache = applicationContext.getBean(WarInfoCache.class);
    warCategoryCache = applicationContext.getBean(WarCategoryCache.class);
    pictureInfoCache = applicationContext.getBean(PictureInfoCache.class);
  }

  @Override
  public Excel transformExcel(Excel excel) {
    Map<String, Object> params = getParams();
    Object id = params.get("cateId");
    if(null != id) {
      WarCategoryResponseDto dto = this.warCategoryCache.getWarCategory(Long.valueOf(id.toString()));
      params.put("name", dto.getName());
      params.put("now", DateUtils.formatTodayDateTime());
      excel = ExcelUtil.fullExcel(excel, params);  //一定注意写在这一行
      List<String> fields = new ArrayList<>();
      Stream.of(excel.getFields()).forEach(field -> fields.add(field));
      fields.addAll(dto.getFields());
      excel.setFields(fields.toArray(new String[0]));
    }
    return excel;
  }

  @Override
  public List<String[]> getRecordsData(Map<String, Object> params, int pageNumber, int pageSize) {
    WarInfoRequestDto warInfoRequestDto = new WarInfoRequestDto();
    Object id = params.get("cateId");
    if(null != id) {
      warInfoRequestDto.setCateId(Long.valueOf(id.toString()));
    }else {
      warInfoRequestDto.setCateId(0L);
    }
    WarCategoryResponseDto cate = this.warCategoryCache.getWarCategory(warInfoRequestDto.getCateId());
    if(null == cate) {
      return new ArrayList<>();
    }
    List<String> fields = cate.getFields();
    Page pager = new Page(pageNumber, pageSize, null, null);
    warInfoRequestDto.setPager(pager);
    List<WarInfoResponseDto> list = this.warInfoCache.queryWarInfo(warInfoRequestDto);
    if(null != list && ! list.isEmpty()) {
      return list.stream().map(dto -> {
        List<PictureInfo> pics = this.pictureInfoCache.getByBusiness(PictureBusinessEnum.zzt.getBusiness(), dto.getId());
        String pic = "";
        if(null != pics && ! pics.isEmpty()) {
          pic = pics.stream().map(p -> p.getUrl()).collect(Collectors.joining(","));
        }
        Map<String, Object> infos = dto.getInfos();
        List<String> values = Collections.synchronizedList(new ArrayList<>());
        values.add(pic);
        values.add(dto.getLng());
        values.add(dto.getLat());
        values.add(dto.getZoom() + "");
        String names = "";
        if(null != dto.getStations() && ! dto.getStations().isEmpty()) {
          names = dto.getStations().stream().collect(Collectors.joining(","));
        }
        values.add(names);
        fields.stream().forEach(field -> {
          Object val = null == infos ? null : infos.get(field);
          if(null == val) {
            val = "";
          }
          values.add(String.valueOf(val));
        });
        return values.toArray(new String[0]);
      }).collect(Collectors.toList());
    }
    return null;
  }
  
  public int getPageSize() {
    return 2000;
  }
}
